package com.example.hello.service.impl;

import com.example.hello.mapper.BookMapper;
import com.example.hello.mapper.UserMapper;
import com.example.hello.pojo.Book;
import com.example.hello.pojo.PageQueryParam;
import com.example.hello.pojo.PageResult;
import com.example.hello.pojo.UserBorrowBookInfo;
import com.example.hello.service.AdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 根据id删除图书信息
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        bookMapper.deleteById(id);
    }

    /**
     * 更新图书信息
     * @param book
     */
    @Override
    public void update(Book book) {
        book.setUpdateTime(LocalDateTime.now());
        bookMapper.update(book);
    }

    /**
     * 查询所有用户的图书借阅情况
     * @return
     */
    @Override
    public PageResult<UserBorrowBookInfo> findUserBorrowInfoList(PageQueryParam pageQueryParam) {
        // 1. 设置分页参数
        PageHelper.startPage(pageQueryParam.getPage(), pageQueryParam.getPageSize());
        // 2. 执行查询
        List<UserBorrowBookInfo> userBorrowBookInfoList = userMapper.findAllUserBorrowBookInfo(pageQueryParam);
        // 3. 解析查询结果，封装为结果对象并返回
        Page<UserBorrowBookInfo> p = (Page<UserBorrowBookInfo>) userBorrowBookInfoList;
        Long total = p.getTotal();
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    /**
     * 修改用户状态
     * @param id
     * @param status
     */
    @Override
    public void updateUserStatus(Integer id, Integer status) {
        userMapper.updateUserStatus(id,status);
    }
}
